package cn.iocoder.yudao.module.handguard.controller.admin.productaccessitem;

import cn.iocoder.yudao.module.handguard.dal.dataobject.usertablesaudit.UserTablesAuditDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.admin.productaccessitem.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.productaccessitem.ProductAccessItemDO;
import cn.iocoder.yudao.module.handguard.convert.productaccessitem.ProductAccessItemConvert;
import cn.iocoder.yudao.module.handguard.service.productaccessitem.ProductAccessItemService;

@Tag(name = "管理后台 - 产品准入项")
@RestController
@RequestMapping("/hutai/product-access-item")
@Validated
public class ProductAccessItemController {

    @Resource
    private ProductAccessItemService productAccessItemService;

    @PostMapping("/create")
    @Operation(summary = "创建产品准入项")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-item:create')")
    public CommonResult<Long> createProductAccessItem(@Valid @RequestBody ProductAccessItemCreateReqVO createReqVO) {
        return success(productAccessItemService.createProductAccessItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品准入项")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-item:update')")
    public CommonResult<Boolean> updateProductAccessItem(@Valid @RequestBody ProductAccessItemUpdateReqVO updateReqVO) {
        productAccessItemService.updateProductAccessItem(updateReqVO);
        return success(true);
    }

    @PostMapping("/saveBatch")
    @Operation(summary = "新增产品准入项")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-item:update')")
    public CommonResult<Boolean> saveProductAccessItemBatch(@Valid @RequestBody ProductAccessItemUpdateBatchReqVO updateReqVO) {
        productAccessItemService.saveProductAccessItemBatch(updateReqVO);
        return success(true);
    }

//    @PostMapping("/updateBatch")
//    @Operation(summary = "批量更新产品准入项")
//    @PreAuthorize("@ss.hasPermission('hutai:product-access-item:update')")
//    public CommonResult<Boolean> updateProductAccessItemBatch(@Valid @RequestBody ProductAccessItemUpdateBatchReqVO updateReqVO) {
//        productAccessItemService.updateProductAccessItemBatch(updateReqVO);
//        return success(true);
//    }

    @DeleteMapping("/delete")
    @Operation(summary = "根据产品准入ID-删除产品准入项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:product-access-item:delete')")
    public CommonResult<Boolean> deleteProductAccessItem(@RequestParam("id") Long id) {
        productAccessItemService.deleteProductAccessItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品准入项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-item:query')")
    public CommonResult<ProductAccessItemRespVO> getProductAccessItem(@RequestParam("id") Long id) {
        ProductAccessItemDO productAccessItem = productAccessItemService.getProductAccessItem(id);
        return success(ProductAccessItemConvert.INSTANCE.convert(productAccessItem));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品准入项列表")
    @Parameter(name = "id", description = "产品准入id", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-item:query')")
    public CommonResult<List<ProductAccessItemRespVO>> getProductAccessItemList(@RequestParam("id") Long id) {
        List<ProductAccessItemRespVO> list = productAccessItemService.getProductAccessItemList(id);
        return success(list);
    }

//    @GetMapping("/dataList")
//    @Operation(summary = "获得产品准入列表分组数据详情")
//    @Parameter(name = "id", description = "产品准入id", required = true, example = "1")
//    @PreAuthorize("@ss.hasPermission('hutai:order:query')")
//    public CommonResult<Map<String,List<ProductAccessItemDO>>> getProductAccessItemGroupList(Long id) {
//        Map<String,List<ProductAccessItemDO>> listMap = productAccessItemService.getProductAccessItemGroupList(id);
//        return success(listMap);
//    }


    @GetMapping("/page")
    @Operation(summary = "获得产品准入项分页")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-item:query')")
    public CommonResult<PageResult<ProductAccessItemRespVO>> getProductAccessItemPage(@Valid ProductAccessItemPageReqVO pageVO) {
        PageResult<ProductAccessItemDO> pageResult = productAccessItemService.getProductAccessItemPage(pageVO);
        return success(ProductAccessItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品准入项 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-item:export')")
    @OperateLog(type = EXPORT)
    public void exportProductAccessItemExcel(@Valid ProductAccessItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProductAccessItemDO> list = productAccessItemService.getProductAccessItemList(exportReqVO);
        // 导出 Excel
        List<ProductAccessItemExcelVO> datas = ProductAccessItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品准入项.xls", "数据", ProductAccessItemExcelVO.class, datas);
    }

}
